package com.mtt.erp.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.mtt.erp.common.domain.QueryRequest;
import com.mtt.erp.common.exception.MttErpException;
import com.mtt.erp.common.utils.MttErpUtil;
import com.mtt.erp.common.utils.SortUtil;
import com.mtt.erp.order.domain.MbaFile;
import com.mtt.erp.order.dao.MbaFileMapper;
import com.mtt.erp.order.domain.MbbFile;
import com.mtt.erp.order.dto.MbaFileDto;
import com.mtt.erp.order.dto.MbaFileShipmentDto;
import com.mtt.erp.order.service.IMbaFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mtt.erp.order.service.IMbbFileService;
import com.mtt.erp.order.vo.MbaFileVo;
import com.mtt.erp.order.vo.MbbFileVo;
import com.mtt.erp.order.vo.OrderDetailVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author guomengzhou
 */
@Service
@Slf4j
public class MbaFileServiceImpl extends ServiceImpl<MbaFileMapper, MbaFile> implements IMbaFileService{

    @Autowired
    private IMbbFileService iMbbFileService;

    @Override
    public IPage<MbaFileVo> orderList(QueryRequest request, MbaFileDto mbaFileDto) {
        LambdaQueryWrapper<MbaFile> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(mbaFileDto.getMba01())) {//订单编号
            queryWrapper.eq(MbaFile::getMba01, mbaFileDto.getMba01());
        }
        if (StringUtils.isNotBlank(mbaFileDto.getMba08())) {//订单来源
            queryWrapper.eq(MbaFile::getMba08, mbaFileDto.getMba08());
        }
        if (StringUtils.isNotBlank(mbaFileDto.getMba09())) {//订单状态
            queryWrapper.eq(MbaFile::getMba09, mbaFileDto.getMba09());
        }

        if (StringUtils.isNotBlank(mbaFileDto.getMba18())) {//收货人
            queryWrapper.like(MbaFile::getMba18, mbaFileDto.getMba18());
        }

        if (StringUtils.isNotBlank(mbaFileDto.getCreateTimeFrom()) && StringUtils.isNotBlank(mbaFileDto.getCreateTimeTo())) {
            queryWrapper.ge(MbaFile::getMbacdate, mbaFileDto.getCreateTimeFrom())
                    .le(MbaFile::getMbacdate, mbaFileDto.getCreateTimeTo());
        }

        //删选当前用户数据
        queryWrapper.eq(MbaFile::getMbacuser, MttErpUtil.getCurrentUser().getUserId());
        Page<MbaFile> page = new Page<>(request.getPageNum(), request.getPageSize());
        SortUtil.handlePageSort(request, page, true);
        IPage<MbaFile> mbaFileIPage = this.page(page, queryWrapper);
        if(mbaFileIPage.getTotal()>0){
            // 把mbaFile的分页结果转化为mbaFileVo的分页结果
            List<MbaFile> mbaFiles = mbaFileIPage.getRecords();
            List<MbaFileVo> mbaFileVos = Lists.newArrayList();
            for (MbaFile mbaFile: mbaFiles) {
                MbaFileVo mbaFileVo = MbaFileVo.builder().build();
                BeanUtils.copyProperties(mbaFile,mbaFileVo);
                mbaFileVos.add(mbaFileVo);
            }
            Page<MbaFileVo> mbaFileVoPage = new Page<>(request.getPageNum(), request.getPageSize());
            BeanUtils.copyProperties(mbaFileIPage,mbaFileVoPage);
            mbaFileVoPage.setRecords(mbaFileVos);
            return mbaFileVoPage;
        }else{
            return null;
        }
    }

    @Override
    public OrderDetailVo orderDetail(String mba00) {
        // 查出订单基本信息
        MbaFile mbaFile = this.getById(mba00);//订单详情
        if(null != mbaFile){
            // 定义返回值
            OrderDetailVo orderDetailVo = OrderDetailVo.builder().build();
            MbaFileVo mbaFileVo = MbaFileVo.builder().build();
            // 如存在mbaFile 吧订单基本信息转化为vo对象
            BeanUtils.copyProperties(mbaFile,mbaFileVo);
            orderDetailVo.setMbaFile(mbaFileVo);

            // 根据订单中商品的外键查询订单中包含的商品并遍历商品的集合把每个商品的值转化为vo对象
            List<MbbFile>  mbbFileList = this.iMbbFileService.mbbListByMbb01(mbaFile.getMba01());
            List<MbbFileVo> mbbFileVos = Lists.newArrayList();
            for (MbbFile mbbFile : mbbFileList) {
                MbbFileVo mbbFileVo = MbbFileVo.builder().build();
                BeanUtils.copyProperties(mbbFile,mbbFileVo);
                mbbFileVos.add(mbbFileVo);
            }
            orderDetailVo.setMbbFileList(mbbFileVos);
            return orderDetailVo;
        }else{
            return null;
        }
    }

    @Override
    public MbaFileVo getOrderInfo(String mba00) {
        MbaFile mbaFile = this.getById(mba00);
        if(mbaFile != null){
            MbaFileVo mbaFileVo = MbaFileVo.builder().build();
            BeanUtils.copyProperties(mbaFile,mbaFileVo);
            return mbaFileVo;
        }else{
            return null;
        }
    }

    @Override
    @Transactional
    public Boolean mbaFileShipment(MbaFileShipmentDto mbaFileShipmentDto) throws MttErpException {
        MbaFile mbaFile = MbaFile.builder().build();
        BeanUtils.copyProperties(mbaFileShipmentDto,mbaFile);
        Long currentUserId = MttErpUtil.getCurrentUser().getUserId();
        mbaFile.setMba09(2);
        mbaFile.setMba29(LocalDateTime.now());
        mbaFile.setMbamdate(LocalDateTime.now());
        mbaFile.setMbamuser(currentUserId.toString());
        try {
            return this.updateById(mbaFile);
        }catch (Exception e){
            log.error("订单发货失败");
            throw new MttErpException("订单发货失败");
        }
    }
}
